package com.roc.steelend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.user.UpdateMsgVo;
import com.roc.steelend.mapper.EmployeeMapper;
import com.roc.steelend.mapper.UserMapper;
import com.roc.steelend.pojo.Employee;
import com.roc.steelend.pojo.User;
import com.roc.steelend.service.UserService;
import com.roc.steelend.util.Md5Util;
import com.roc.steelend.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.jws.soap.SOAPBinding;
import javax.management.QueryEval;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectList(null);
    }

    @Override
    public List<User> selectAllUser2() {
        return userMapper.selectAllUser2();
    }

    @Override
    public Boolean insertUser(User user,Employee employee) {
        int employeeId = UuidUtil.UuidWay();
        employee.setEmployeeId(employeeId);
        int employeeInsert = employeeMapper.insert(employee);
        if (employeeInsert==1){
            user.setEmployeeId(employeeId);

            String password = user.getPassword();
                user.setPassword(Md5Util.md5JiaMi(password));
            int userInsert = userMapper.insert(user);
            if (userInsert==1) {
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }


    }

    @Override
    public User loginUser(User user) {
        String password = user.getPassword();
        user.setPassword(Md5Util.md5JiaMi(password));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",user.getUserName());
        wrapper.eq("password",user.getPassword());
        List<User> users = userMapper.selectList(wrapper);
        if (users.isEmpty()){
            return null;
        }else {
            return users.get(0);
        }
    }

    @Override
    public Boolean changePassword(Integer userId, String newPassword) {
        User user = new User();
        String md5Password = Md5Util.md5JiaMi(newPassword);
        user.setUserId(userId);
        user.setPassword(md5Password);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        int update = userMapper.update(user, wrapper);
        return update == 1;
    }

    @Override
    public Boolean changePasswordBefore(Integer userId, String oldPassword) {
        String md5Password = Md5Util.md5JiaMi(oldPassword);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("password",md5Password);
        List<User> users = userMapper.selectList(wrapper);
        return !users.isEmpty();
    }

    @Override
    public Integer checkUserPower(Integer userId) {
        return userMapper.checkUserPower(userId);
    }

    @Override
    public String checkUserRealName(Integer userId) {
        return userMapper.checkUserRealName(userId);
    }

    @Override
    public Integer checkUserEmployeeId(Integer userId) {
        return userMapper.checkUserEmployeeId(userId);
    }

    @Override
    public List<HashMap<String, Object>> userChangeMsgDetail(Integer employeeId) {
        return userMapper.userChangeMsgDetail(employeeId);
    }

    @Override
    public Boolean userCheckName(String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        List<User> users = userMapper.selectList(wrapper);
        if (users.isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public String userOldName(Integer employeeId) {

        return userMapper.userOldName(employeeId);
    }

    @Override
    public ReturnMsg updateUserMsg(UpdateMsgVo updateMsgVo,Integer employeeId) {
//        更新用户表的数据
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("employee_id",employeeId);
        User user = userMapper.selectList(userQueryWrapper).get(0);
        user.setUserName(updateMsgVo.getUserName());
        user.setEmail(updateMsgVo.getEmail());
        int updateById1 = userMapper.updateById(user);
//        更新员工表的数据
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("employee_id",employeeId);
        Employee employee = employeeMapper.selectList(employeeQueryWrapper).get(0);
        employee.setSex(updateMsgVo.getSex());
        employee.setPhone(updateMsgVo.getPhone());
        employee.setRealName(updateMsgVo.getRealName());
        int updateById = employeeMapper.updateById(employee);
        ReturnMsg msg = new ReturnMsg();
        if (updateById==1&&updateById1==1){
            msg.setCode(1);
            msg.setMsg("更新用户数据成功");

        }else {
            msg.setCode(0);
            msg.setMsg("更新用户数据失败,请重试");

        }
        return  msg;
    }

    @Override
    public List<HashMap<String, Object>> test3Token(Integer employeeId) {


        return userMapper.test3Token(employeeId);
    }
}
